define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree', 'vue'], function ($, undefined, Backend, Table, Form, undefined, Vue) {
//读取选中的条目
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user_list',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                search: false,
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
                        {
                            field: 'user_type',
                            title: __('User type'),
                            formatter: function (value, row, index) {
                                value = value + '';
                                switch (value) {
                                    case '0':
                                        return __('Normal User');
                                    case '1':
                                        return __('Member User');
                                }
                            },
                            searchList: {
                                '0': __('Normal User'),
                                '1': __('Member User')
                            },
                            style: 'min-width:100px;'
                        },
                        {
                            field: 'is_manager',
                            title: __('Is manager'),
                            events: Controller.api.events.deal,
                            formatter: function (value, row, index) {
                                value = value + '';
                                switch (value) {
                                    case '0':
                                        return '<a class="btn btn-default btn-xs btn-deal">' + __('No') + '</a>';
                                    case '1':
                                        return '<a class="btn btn-default btn-xs btn-dealed">' + __('Yes') + '</a>';
                                }
                            },
                            searchList: {
                                '0': __('No'),
                                '1': __('Yes')
                            },
                            style: 'min-width:100px;'
                        },
                        {
                            field: 'create_time',
                            title: __("Createtime"),
                            formatter: Table.api.formatter.datetime,
                            searchable: false
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'change_type',
                                    text: __('Change type'),
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'user/user_list/change_type'
                                }
                            ],
                            formatter: function (value, row, index) {
                                if (row.user_type == 0) {
                                    return Table.api.formatter.operate.call(this, value, row, index);
                                }

                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        change_type: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"), null, null, function () {
                    if ($("#treeview").size() > 0) {
                        var r = $("#treeview").jstree("get_all_checked");
                        $("input[name='row[rules]']").val(r.join(','));
                    }
                    return true;
                });
                //全选和展开
                $(document).on("click", "#checkall", function () {
                    $("#treeview").jstree($(this).prop("checked") ? "check_all" : "uncheck_all");
                });
                $(document).on("click", "#expandall", function () {
                    $("#treeview").jstree($(this).prop("checked") ? "open_all" : "close_all");
                });

            },
            events: {
                deal: {
                    'click .btn-deal': function (e, value, row, index) {
                        Layer.confirm("是否将该用户提升为管理，将会收到微信公众号的审核信息推送", function () {
                            $.ajax({
                                type: "post"
                                , url: "user/user_list/being_admin"
                                , data: {
                                    id: row.id
                                }
                                , error: function (data) {
                                    console.info(data);
                                    Layer.msg("服务器异常");
                                }
                                , success: function (data) {
                                    console.info(data);
                                    if (data.status == 1) {
                                        Layer.msg(data.msg, function () {
                                            $("#table").bootstrapTable('refresh');
                                        });
                                    }
                                    else {
                                        Layer.msg(data.msg);
                                    }
                                }
                            });
                        });
                    },
                    'click .btn-dealed': function (e, value, row, index) {
                        Layer.confirm("是否取消管理员", function () {
                            $.ajax({
                                type: "post"
                                , url: "user/user_list/cancel_admin"
                                , data: {
                                    id: row.id
                                }
                                , error: function (data) {
                                    console.info(data);
                                    Layer.msg("服务器异常");
                                }
                                , success: function (data) {
                                    console.info(data);
                                    if (data.status == 1) {
                                        Layer.msg(data.msg, function () {
                                            $("#table").bootstrapTable('refresh');
                                        });
                                    }
                                    else {
                                        Layer.msg(data.msg);
                                    }
                                }
                            });
                        });
                    }
                }
            }
        }
    };
    return Controller;
});